#include "AllegroWindow.h"

CAllegroWindow::CAllegroWindow(void)
{
	m_wb=NULL;
	m_text = NULL;
	m_x1=m_y1=m_x2=m_y2=0;
	m_background=NULL;
}

CAllegroWindow::~CAllegroWindow(void)
{
	DestroyWindow();
}

void CAllegroWindow::SetText( const char* text )
{
	DeleteText();
	m_text = strdup(text);
}

void CAllegroWindow::SetRect( int x1, int y1, int x2, int y2 )
{
	if(m_wb!=NULL)
		destroy_bitmap(m_wb);
	m_wb = create_bitmap(x2-x1,y2-y1);

	m_x1=x1;
	m_x2=x2;
	m_y1=y1;
	m_y2=y2;
}

void CAllegroWindow::DeleteText()
{
	if(m_text!=NULL)
		delete m_text;
	m_text = NULL;
}

void CAllegroWindow::Draw( BITMAP* sb )
{
	blit(m_wb,sb,0,0,m_x1,m_y1,m_wb->w,m_wb->h);
}

void CAllegroWindow::SetFont( ALFONT_FONT* font )
{
	m_font = font;
}

bool CAllegroWindow::ContainsText()
{
	if(m_text==NULL)
		return false;
	if(m_text[0]==0)
		return false;
	return true;
}

void CAllegroWindow::SetBG( const char* filename )
{
	if(m_background!=NULL)
		destroy_bitmap(m_background);
	m_background = load_bitmap(filename,NULL);
	if(m_background!=NULL)
		this->RedrawBuffer();
}

void CAllegroWindow::DestroyWindow()
{
	DeleteText();
	if(m_wb!=NULL)
		destroy_bitmap(m_wb);
	m_wb=NULL;
	if(m_background!=NULL)
		destroy_bitmap(m_background);
	m_background=NULL;
}

void CAllegroWindow::RedrawBuffer()
{
	if(m_background!=NULL)
		stretch_blit(m_background,m_wb,0,0,m_background->w,m_background->h,0,0,m_wb->w,m_wb->h);
	else
		clear_to_color(m_wb,makecol(0,0,0));
}

bool CAllegroWindow::isInside( int px,int py )
{
	if(px>m_x1 && px<m_x2 && py>m_y1 && py<m_y2)
		return true;
	else
		return false;
}

void CAllegroWindow::Move( int px, int py )
{
	m_x2=px+(m_x2-m_x1);
	m_y2=py+(m_y2-m_y1);
	m_x1=px;
	m_y1=py;
}